import random
from util.CmsAutoMation import CmsAutoMation
from DrissionPage import ChromiumPage,ChromiumOptions
import pandas as pd

USER_AGENTS = [
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36',
    'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36',
    'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
    'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
]
headers = {
    #随机选择一个用户代理
    random.choice(USER_AGENTS)
}

co = ChromiumOptions()
# 设置header
co.set_user_agent(headers)
# 阻止“自动保存密码”的提示气泡
co.set_pref('credentials_enable_service', False)
# 阻止“要恢复页面吗？Chrome未正确关闭”的提示气泡
co.set_argument('--hide-crash-restore-bubble')
# 禁用沙盒
co.set_argument('--no-sandbox')
# 无痕
co.incognito(True)
# 创建页面对象，并启动或接管浏览器
page = ChromiumPage(co)
# 无头
co.headless(True)
# 创建页面对象，并启动或接管浏览器
page = ChromiumPage(co)
mcsAmt = CmsAutoMation(page)
# 出现的弹窗都是确认
page.set.auto_handle_alert(all_tabs=True)

def main():
    __name__ = "__main__"
    url  ='https://www.jiangshuntech.com/admin.php'
    username = 'admin'
    password = 'qq123456'
    seo_xfile = r"E:\project\py-pro\automation\zidong\documents\cms_seo.xlsx"
    
    article_release(url,username,password,seo_xfile)
    page.quit()

def article_release(url,username,password,seo_xfile):
    mcsAmt.start(url,username,password)
    mcsAmt.article_manage()
    # 读取本地excel文件
    data = pd.read_excel(seo_xfile)
    for i in range(len(data)):
        if data['id'][i] >=0:
            seo_data={'id':data['id'][i],'title':data['t10'][i],'keywords':data['keywords'][i],'litpic':data['litpic'][i]}
            mcsAmt.article_copy(seo_data)
            if(i%15==0):
                # 刷新页面
                page.refresh()
                page.wait(1.5)
                continue

if __name__ == "__main__":
    main()